package week4.num.ExceptionDemo01.neueduTotal.card;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CardTest {
    public static void main(String[] args) {
        play();
    }

    public static void play() {
        List<Card> cards =createCards();
        createPlayers(cards);
        System.out.println("底牌为:" + cards.get(cards.size() - 3) +
                cards.get(cards.size() - 2) +
                cards.get(cards.size() - 1));
    }

    /**
     * 创建所有牌
     * 创建集合并利用循环将52张基本牌添加到集合中
     */
    private static List<Card> createCards() {
        List<Card> cards = new ArrayList<>();
        for (int suit = Card.DIAMOND; suit < Card.JOKER; suit++) {
            for (int rank = Card.THREE; rank < Card.BLACK; rank++) {
                Card card = new Card(suit, rank);
                cards.add(card);
            }
        }
        //向牌组中添加小王
        cards.add(new Card(Card.JOKER, Card.BLACK));
        //向牌组中添加大王
        cards.add(new Card(Card.JOKER, Card.COLOR));
        //打乱牌的顺序
        Collections.shuffle(cards);
        System.out.println(cards);
        return cards;
    }


    /**
     * 创建集合存入所有玩家
     * 并发牌
     */
    private static void createPlayers(List<Card> cards) {
        List<Player> player = new ArrayList<>();
        player.add(new Player("张三", "123123"));
        player.add(new Player("李四", "123124"));
        player.add(new Player("王五", "123125"));
        //发牌
        for (int i = 0; i < cards.size() - 3; i++) {
            player.get(i % 3).add(cards.get(i));
        }
        //进行排序
        for (int i=0;i< player.size();i++) {
            Arrays.sort(player.get(i).cards);
        }
        player.forEach(System.out::println);
    }
}
